package com.calabar.phm.etl.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

/**
 * @Author zmc <mingcheng.zhang@cdcalabar.com>
 * @Date 17-6-8 上午11:21
 * @Version v1.0
 * @Des 调用spark驱动器测试程序
 */
public class Main {

    public static void main(String[] args) {

        String[] commands = new String[]{"bin/start.sh", args[1], args[2], args[3],args[4]};
        ProcessBuilder processBuilder = new ProcessBuilder(commands).directory(new File(args[0]));
        processBuilder.redirectErrorStream(true);
        printCommands(commands);
        Process process = null;
        try {
            process = processBuilder.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            StringBuilder sb = new StringBuilder();
                String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n\r");
            }
            br.close();
            String runningLog = sb.toString();
            if (!runningLog.isEmpty()) {
                System.out.println(runningLog);
            }
            process.waitFor();
        } catch (Exception ex) {
            System.err.println(ex);
        }

    }

    private static void printCommands(String[] commands) {
        String cmd = "";
        for (String c : commands) {
            if (!cmd.isEmpty()) {
                cmd = cmd + " " + c;
            } else {
                cmd = c;
            }
        }
        System.out.println("\nThe commonds:" + cmd);
    }

}
